{{left_sidebar_enabled,right_sidebar_enabled=False,True}}
{{extend 'layout.html'}}

{{if 'message' in globals():}}
<h4>{{=message}}</h4>
<div id='refresh_bar' style='background-color:#C0C0C0;'>
    <center>
        {{=A('Twitts', _href='#', _onclick='refresh()', _style='display:none;')}}  
    </center>        
</div>
<br/>
{{=LOAD(c='default', f='fav_tweets', target='tweets', ajax=True) }}

{{else:}}
{{=BEAUTIFY(response._vars)}}
{{pass}}
<br/>
{{block right_sidebar}}
<!--
<h6>{{=T("Don't know what to do?")}}</h6>
<ul>
  <li>{{=A(T("Online examples"), _href=URL('examples','default','index'))}}</li>
  <li><a href="http://web2py.com">web2py.com</a></li>
  <li><a href="http://web2py.com/book">{{=T('Documentation')}}</a></li>
</ul>
-->

{{end}}

<script>

    $(document).ready(function() {           
          loadTagCloud();          
          interval = setInterval(check, 10*60000);  // every minutes  
          
          // hide the flash message:
          $('.flash').fadeOut();                                                         
         
    });

    function spam(_id) {
       
        console.debug('id: ' + _id);
        val = $('div #' + _id).find('#text').text();
        console.debug(val);
                             
        $.post("/{{=request.application}}/default/spam", { id: _id, text:val },
           function(data){             
             console.debug("Data Loaded: " + data);
             if(data == 'ok') {
                 // $('div #' + _id).focus();
                 $(window).scrollTop($('div #' + _id).position().top)
                 $('div #' + _id).fadeOut();                 
             }
        });
        
    }
    
    function refresh() { 
        var title = "{{=response.title}}";
        $.get("/{{=request.application}}/default/refresh",{},
            function(data) {                
                if (data!=null && data != 'None') {                    
                     $('div #tweets').prepend(data);                     
                     document.title = title;
                     $('#refresh_bar a').hide();           
                }
            });
    }
    
     function check() {
         var title = "{{=response.title}}";
         $.get("/{{=request.application}}/default/check_new",{},
            function(data) {
                if (data!=null && data > 0) {
                    console.debug(data);
                    document.title = "(" + data + ") " + title;
                    $('#refresh_bar a').text(data + " new Tweets");
                    $('#refresh_bar a').show();
                }
            });
     }
     
    <!-- A set of function to create the tag cloud -->
    
    function createRandomColor() {
        var hex = '0123456789ABC'.split(''), 
        color = '#', i;
 
        for (i = 0; i < 6; i += 1) { 
            color = color + hex[Math.floor(Math.random() * 13)]; 
        } 
 
        return color; 
    }

    function setFontSize() {
 
        var maxFontSize = 15;
        var fontSize = Math.floor(Math.random() * maxFontSize + 14) + 'px';
 
        return fontSize;
 
    }

    function setOffsets() {
 
        var offsets = {};
        
        var randTop = Math.floor(Math.random() * 10);
        var randLeft = Math.floor(Math.random() * 10);
 
        var maxTop = Math.floor(Math.random() * randTop) + 'px';
        var maxLeft = Math.floor(Math.random() * randLeft) + 'px';
 
       offsets.top = maxTop;
       offsets.left = maxLeft;
 
       return offsets;   
 
    }

    function loadTagCloud() {

      $.get("/{{=request.application}}/default/tag_cloud",{},
            function(data) {
                if (data) {                    
                    $('div.four.columns').prepend(data);
                    
                     $('#tag_cloud li').each(function() {
                       var $a = $(this).find('a');
                       var cssColor = createRandomColor();
                       var cssFontSize = setFontSize();
 
                       var linkOffsets = setOffsets();                       
                                         
                       $a.css({color: cssColor, fontSize: cssFontSize, top: linkOffsets.top, left: linkOffsets.left});
                        
                   });                                                       
                }
           });
   }
   
   function post() {
   
   }
         
</script>
